﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace HeroldIT.Dnn.Modules
{
    /// <summary>
    /// Holds the entries of a paged request to a list of items.
    /// </summary>
    /// <typeparam name="T">The type of the list's entries.</typeparam>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
    public class PagedList<T> : ReadOnlyCollection<T>
    {
        #region properties

        /// <summary>
        /// Gets the list's total entry count.
        /// </summary>
        public int TotalCount
        {
            get; private set;
        }

        #endregion

        #region constructors

        /// <summary>
        /// Creates a paged list with the given total item count and items.
        /// </summary>
        /// <param name="totalCount">The total item count of the list.</param>
        /// <param name="items">The items for the requested page.</param>
        public PagedList(int totalCount, IEnumerable<T> items)
            : this(totalCount, null == items ? null : items.ToList())
        {
        }

        /// <summary>
        /// Creates a paged list with the given total item count and items.
        /// </summary>
        /// <param name="totalCount">The total item count of the list.</param>
        /// <param name="items">The items for the requested page.</param>
        public PagedList(int totalCount, IList<T> items)
            : base(items ?? new T[0])
        {
            this.TotalCount = totalCount;
        }

        #endregion

        #region methods

        /// <summary>
        /// Gets whether this <see cref="PagedList{T}"/> has a next page.
        /// </summary>
        /// <param name="page">The index of the current page starting at <c>0</c>.</param>
        /// <param name="pageSize">The size of the pages.</param>
        /// <returns>Whether this <see cref="PagedList{T}"/> has a next page.</returns>
        public bool HasNextPage(int page, int pageSize)
        {
            return this.TotalCount > (page + 1) * pageSize;
        }

        #endregion
    }
}
